unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ElXPThemedControl, ElTree;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ElTree1: TElTree;
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var
  I,M,N:Integer;
  ItemA:TElTreeItem;
  ItemB:TElTreeItem;
  ItemC:TElTreeItem;
begin
  //
  with ElTree1 do
  begin
    ShowCheckboxes:=True;
    Items.BeginUpdate;
    for I:=0 to 5 do
    begin
      ItemA:=Items.Add(nil,Format('0000%d',[I]));
      ItemA.ShowCheckBox:=True;

    end;

    ItemA:=Items.Item[3];

    for I:=0 to 2 do
    begin
      ItemB:=Items.AddChild(ItemA,Format('child:000%d',[I]));
      ItemB.ShowCheckBox:=True;

      if I=1 then
      begin
        for M:=0 to 2 do
        begin
          ItemC:=Items.AddChild(ItemB,Format('next:000%d',[M])) ;
          ItemC.ShowCheckBox:=True;
        end;  
      end;  
    end;
    FullExpand;
    Items.EndUpdate;
  end;  
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  StrA:string;
  I:Integer;
  ItemA:TElTreeItem;
  StrsIdex:TStringList;

  procedure ReadNext(AItem:TElTreeItem);
  var
    M,N:Integer;
    ItemB:TElTreeItem;
  begin
    Writeln('Levl:'+AItem.Text);
    if AItem.Count=0 then Exit;
    for M:=0 to AItem.Count-1 do
    begin
      ItemB:=AItem.Item[M];
      if ItemB.Checked then
      begin
        StrsIdex.Add(ItemB.Text);
        ReadNext(ItemB);
      end;  
    end;  
  end;

  function ReadItem(AItem:TeltreeItem):string;
  var
    M,N:Integer;
    ItemB:TElTreeItem;
    StrB :string;
  begin
    Result:=AItem.Text;
    N:=0;
    StrB:='';
    for M:=0 to AItem.Count-1 do
    begin
      ItemB:=AItem.Item[M];
      if ItemB.Checked then
      begin
        Inc(N);
        StrB:=StrB+','+ItemB.Text;
        StrsIdex.Add(ItemB.Text);
        ReadNext(ItemB);
        
      end;  
    end;

    if N <> AItem.Count then
    begin
      Delete(StrB,1,1);
      StrB:='('+StrB+')';
      Result:=Result+StrB
    end;

  end;  

begin
  StrA:='';
  StrsIdex:=TStringList.Create;
  
  with ElTree1 do
  begin
    for I:=0 to Items.Count-1 do
    begin
      ItemA:=Items.Item[i];
      if not ItemA.Checked then Continue;
      if StrsIdex.IndexOf(ItemA.Text)<>-1 then Continue;
      if ItemA.HasChildren then
      begin
        StrA:=StrA+','+ReadItem(ItemA);
      end else
      begin
        StrA:=StrA+','+Itema.Text;
      end;
    end;

    Writeln(StrA);
  end;  

end;

end.
